// pages/login/loginPhone.js
const app = getApp()
import {
  sendMsg,
  msgLogin
} from '../../api/user'
import {
  lockSysUser
} from '../../api/member'
import utils from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chooseType: false,
    phoneNumber: '',
    vCode: '',
    isShow: true,
    second: 60,
    vCodeShow:false
  },


  yinsi(){
    wx.navigateTo({
      url: '/pages/member/detail/detail?title=1&id=24354 '
    })
  },
  tiaokuang(){
    wx.navigateTo({
      url: '/pages/member/detail/detail?title=2&id=24355'
    })
  },


  verificationCode(e) {
    this.data.vCode = e.detail.value
    // if(this.data.vCode.length==4){
    //   this.data.vCodeShow==
    // }
    // console.log(this.data.vCode.length);
    this.setData({
      vCode:this.data.vCode
    })
  },
  loginClick() {
    var _this = this;
    if (this.data.vCode.length == 4) {
      let mchId = app.globalData.mchId;
      if (app.APP_TYPE > 0) {
        mchId = app.ISV_ID
      }
      let username = 'mch_' + mchId + '_' + this.data.phoneNumber;
      // if (this.data.chooseType == false) {
      //   wx.showToast({
      //     title: '请您先同意隐私政策以及服务协议',
      //     icon: 'none',
      //     duration: 2000
      //   })
      //   return
      // } else {
      //   msgLogin({
      //     mchId: mchId,
      //     username: username,
      //     wxmId: app.globalData.uuid,
      //     verifyCode: this.data.vCode
      //   }).then(res => {
      //     if (res.type == 'success') {
      //       const {
      //         userInfo,
      //         token
      //       } = res.data
      //       app.globalData.token = token
      //       app.globalData.userInfo = userInfo
      //       _this.startLockSysUser()
      //       wx.switchTab({
      //         url: '/pages/index/index'
      //       })
      //     }
      //   }).catch(error => {
      //     wx.showToast({
      //       title: error.content,
      //       icon: 'none',
      //       duration: 2000
      //     })
      //   })
      // }
        msgLogin({
          mchId: mchId,
          username: username,
          wxmId: app.globalData.uuid,
          verifyCode: this.data.vCode
        }).then(res => {
          if (res.type == 'success') {
            const {
              userInfo,
              token
            } = res.data
            app.globalData.token = token
            app.globalData.userInfo = userInfo
            _this.startLockSysUser()
            wx.switchTab({
              url: '/pages/index/index'
            })
          }
        }).catch(error => {
          wx.showToast({
            title: error.content,
            icon: 'none',
            duration: 2000
          })
        })


    } else {
      wx.showToast({
        title: '验证码格式错误',
        icon: 'none',
        duration: 2000
      })
    }
  },
  startLockSysUser() {
    lockSysUser({
      mchId: app.globalData.mchId,
      xuid: app.globalData.xuid,
      shopId: app.globalData.shopId
    }).then(res => {
      console.log(res)
      wx.showToast({
        title: res.content,
        icon: 'none',
        duration: 2000
      })
    })
  },
  inputPhoneNum(e) {
    this.data.phoneNumber = e.detail.value
  },
  verificationClick() {
    if (this.data.phoneNumber == "" || this.data.phoneNumber.length != 11 || !(/^((1))\d{10}$/.test(this.data.phoneNumber))) {
      wx.showToast({
        title: '请输入正确手机号码',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    this.timer()
    let mchId = app.globalData.mchId;
    if (app.APP_TYPE > 0) {
      mchId = app.ISV_ID
    }
    sendMsg({
      username: this.data.phoneNumber,
      tplKey: 'register',
      mchId: mchId,
    }).then(res => {
      console.log(res)
    })
    this.data.isShow = false
    this.setData({
      isShow: this.data.isShow
    })
  },
  chooseClick() {
    this.data.chooseType = !this.data.chooseType
    this.setData({
      chooseType: this.data.chooseType
    })
  },
  timer: function () {
    let promise = new Promise((resolve, reject) => {
      let setTimer = setInterval(
        () => {
          this.setData({
            second: this.data.second - 1
          })
          if (this.data.second <= 0) {
            this.setData({
              second: 60,
              isShow: true,
            })
            resolve(setTimer)
          }
        }, 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


  },

})
